import axios from 'axios'

let http=axios.create({
});

http.interceptors.request.use(req=>{
    let token=sessionStorage.getItem('token');
    token && (req.headers['token']=token);

    return req;
},err=>{
    return Promise.reject(err);
})

http.interceptors.response.use(res=>{ //res是后台的相应
    //拦截的是后台给前端的相应
    //token也是有过期时间的。如果这个后台是快过期了再返回一个新的token
    // res.data.data && sessionStorage.setItem('token',res.data.data);
    return res;
},(err)=>{
    console.log(err.response.status)
    switch(err.response.status){
        case 401:
            sessionStorage.removeItem('token');
            window.location.href='/login';
    }
    return Promise.reject(err);
})

export default http;
